"""
音频播放器抽象类
"""
import queue
import threading

from common.log import logger


class Player(object):
    def __init__(self, output_queue: queue.Queue):
        self.output_queue = output_queue
        self.running = False
        self.thread = threading.Thread(target=self._write, daemon=True)

    def startup(self):
        """播放器启动"""
        logger.error("NotImplementedError")
        raise NotImplementedError

    def shutdown(self):
        """播放器关闭"""
        logger.error("NotImplementedError")
        raise NotImplementedError

    def stop(self):
        """暂停"""
        logger.error("NotImplementedError")
        raise NotImplementedError

    def play(self, data):
        """播放"""
        logger.error("NotImplementedError")
        self.output_queue.put(data)

    def _write(self, data):
        """音频播放线程"""
        while self.running:
            data = self.output_queue.get()
            try:
                self.write(data)
            except Exception as e:
                logger.error(f"Player write error: {e}")
            finally:
                self.output_queue.task_done()

    def write(self, data):
        """音频写入"""
        logger.error("NotImplementedError")
        raise NotImplementedError
